/// <reference name="MicrosoftAjax.debug.js" />
/// <reference path="../../Resources/js/jquery/jquery.debug.js" />
/// <reference path="../Navigation/ItemsNavigator.debug.js />


// CombinedDescriptor object

BM.CombinedDescriptor = function(item, additionNode) {
    BM.CombinedDescriptor.initializeBase(this, [item, additionNode]);
}

BM.CombinedDescriptor.prototype = {
    load: function() {
        BM.CombinedDescriptor.callBaseMethod(this, 'load');
        this.item.descriptor = this;
        this.loadChunks();
        this.getItem().getMForm().add_loadComplete(Function.createDelegate(this, this.raiseDescriptionChanged));        
    },

    loadChunks: function() {
        this.chunks = new Array();
        $(this.additionNode).children().each(
            function(dscr) {
                dscr.chunks[dscr.chunks.length] = new BM.DescriptionChunk[this.className](dscr, this);
            }
            , [this]
        );
    },

    getDescription: function() {
        var builder = new Sys.StringBuilder();
        for (var i = 0; i < this.getChunks().length; i++) {
            builder.append(this.getChunks()[i].toString());
        }
        return builder.toString();
    },

    getChunks: function() {
        return this.chunks;
    }
}

BM.CombinedDescriptor.registerClass("BM.CombinedDescriptor", BM.Descriptor);
BM.Addition['CombinedDescriptor'] = BM.CombinedDescriptor;

// Chunk object

BM.DescriptionChunk = function(descriptor, node) {
    this.descriptor = descriptor;
    this.node = node;
}

BM.DescriptionChunk.registerClass("BM.DescriptionChunk");

//TextChunk object

BM.TextChunk = function(descriptor, node) {
    BM.TextChunk.initializeBase(this, [descriptor, node]);
}

BM.TextChunk.prototype = {
    toString: function() {
        var output = this.node.innerHTML
        if (output == null || output == '') return ' ';
        return output;
    }
}

BM.TextChunk.registerClass("BM.TextChunk", BM.DescriptionChunk);
BM.DescriptionChunk['TextDescription'] = BM.TextChunk;

//ValueChunk object

BM.ValueChunk = function(descriptor, node) {
    BM.ValueChunk.initializeBase(this, [descriptor, node]);
    this.getDescriptorItem().addChanged(Function.createDelegate(this.descriptor, this.descriptor.raiseDescriptionChanged));
}

BM.ValueChunk.prototype = {
    getItem: function() {
        return this.descriptor.item;
    },

    toString: function() {
        return this.getDescriptorItem().getValue();
    },

    getDescriptorItem: function() {
        return BM.ItemsNavigator.selectSingle(this.getItem(), this.getPath());        
    },    

    getPath: function() {
        return this.node.getAttribute('path');
    }

}

BM.ValueChunk.registerClass("BM.ValueChunk", BM.DescriptionChunk);
BM.DescriptionChunk['ValueDescription'] = BM.ValueChunk;

